import QtQuick 2.11
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.11
import FluentUI

/**EMPatientList.qml
  * 管理病人信息的封装组件, 外部组建通过setDataArray控制渲染
  * dataArray是来自数据库的list，包含[{patientName, testId}]
  * addData则是对应计算完成后需要添加的数据
  * 组建通过EMSearchBox的信号完成UI的数据筛查
  * EMIdExpander则负责具体的数据渲染
  *
  * 简单使用:
    Timer {id: timer}
    function setTimeout(cb,delayTime) {
       //timer = new Timer();
       timer.interval = delayTime;
       timer.repeat = false;
       timer.triggered.connect(cb);
       timer.start();
    }
    EMPatientList{
        height: parent.height
        width: 200

        onItemClick: (d1, d2)=>{
            console.log(d1, d2)
        }

        Component.onCompleted: {
            setDataArray([
                {patientName: "tacom", testId: "123"},
                {patientName: "tacom", testId: "12345"},
                {patientName: "ta", testId: "123"},
                {patientName: "t", testId: "1"},
                {patientName: "t111", testId: "1"},
                {patientName: "t2222", testId: "1"}
            ])

            setTimeout(function(){
                addData({patientName: "hhhh", testId: "1"})
            }, 3000)

        }
    }
  */


Item {
    id: root
    property var dataArray: []
    property var nameArray: []
    property string nowName: ""
    signal itemClick(string patientName, string testId)

    Column{
        anchors.fill: parent

        // 搜索框
        EMSearchBox{
            id: searchBox
            width: root.width
            height: 40

            onItemClick: (data)=>{
                nowName = data
                root.setExpanderData()
                // console.log(data)
            }
        }

        // 交互二级列表
        EMIdExpander{
            id: idExpander
            height: parent.height - searchBox.height - 20
            width: root.width
            onItemClick: (res)=>{
                let split_str = res.split("/")
                root.itemClick(split_str[0], split_str[1])  // 数据3
                // console.log(res)
            }
        }
    }

    function setDataArray(dArray){
        dataArray = dArray
        var nameList = []
        for(var i = 0; i < dArray.length; i++){
            nameList.push(dArray[i].patientName)
        }
        nameArray = Array.from(new Set(nameList))


        setSearchData()
        setExpanderData()
    }

    function addData(item){
        dataArray.push(item)
        var nameIndex = findNameIndex(item.patientName)
        if(nameIndex === -1){
            nameArray.push(item.patientName)
        }

        setSearchData()
        setExpanderData()
    }

    function findNameIndex(key){
        for(var i = 0; i < nameArray.length; i++){
            if(nameArray[i] === key){
                return i
            }
        }
        return -1
    }

    function setSearchData(){
        searchBox.appendSearchData(nameArray)
    }

    function setExpanderData(){
        if(nowName === ""){
            idExpander.setModelData(dataArray)
        }else{
            var modelData = []
            for(var i = 0; i < dataArray.length; i++){
                if(dataArray[i].patientName.indexOf(nowName) !== -1){
                    modelData.push(dataArray[i])
                }
            }
            idExpander.setModelData(modelData)
        }
    }
}
